package com.example.testlibraryproject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;

import com.example.testlibraryproject.utils.L;

public class ContactsTestActivity extends Activity {
	
	List<ContactDataItem> contactsDataList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contacts_test);
		
		contactsDataList = new ArrayList<ContactsTestActivity.ContactDataItem>();
		getAllContacts();
		
		//code to sort the array list
		Collections.sort(contactsDataList);
		//so this contactsDataList contains the data values
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contacts_test, menu);
		return true;
	}
	
	public void getAllContacts() {

        Cursor phones = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          L.v("phone::"+name+":"+phoneNumber); 
         contactsDataList.add(new ContactDataItem(name, phoneNumber));
        }

        phones.close();
     }
	
	/**
	 * Class to act as data storage for phone details
	 * @author Dayanand
	 *
	 */
	class ContactDataItem implements Comparable<ContactDataItem>{
		String name;
		String phoneNumber;
		public ContactDataItem(String name, String phoneNumber) {
			super();
			this.name = name;
			this.phoneNumber = phoneNumber;
		}
		
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPhoneNumber() {
			return phoneNumber;
		}

		public void setPhoneNumber(String phoneNumber) {
			this.phoneNumber = phoneNumber;
		}

		@Override
		public int compareTo(ContactDataItem another) {
			// TODO Auto-generated method stub
			return this.name.compareTo(another.getName());
		}
		
		
		
	}
}
